
#include "GameState.h"

#include <vector>
#include <cassert>

static GameState* game_state = NULL;

GameState& GameState::instance(void){
    if (game_state == NULL) {
        game_state = new GameState();
    }
    return *game_state;
}

void GameState::reset(void) {
    delete game_state;
    game_state = NULL;
}


void GameState::addPlayerToTeam(Player *player, unsigned team_id){
    for(unsigned i = 0; i < teams.size(); i++){
        if(teams[i].getTeamId() == team_id){
            teams[i].addPlayer(player);
            return;
        }
    }
    assert(false);
}


int GameState::getTeamScore(unsigned team_id){
    for(unsigned i = 0; i < teams.size(); i++){
        if(teams[i].getTeamId() == team_id){
            return teams[i].getTeamScore();
        }
    }
    assert(false);
    return 0;
}

void GameState::changeTeamScore(unsigned team_id, int score_delta){
    for(unsigned i = 0; i < teams.size(); i++){
        if(teams[i].getTeamId() == team_id){
            teams[i].changeTeamScore(score_delta);
            return;
        }
    }
    assert(false);
}

unsigned GameState::teamIdFromPixelColour(unsigned char r, unsigned char g, unsigned char b){
    for(unsigned i = 0; i < teams.size(); i++){
        unsigned char tr, tg, tb;
        teams[i].getTeamColour(tr, tg, tb);
        if(tr == r && tg == g && tb == b){
            return teams[i].getTeamId();
        }
    }
    assert(false);
    return 0;
}

Team& GameState::getTeam(unsigned team_id){
    assert(teams.size() > 0);
    for(unsigned i = 0; i < teams.size(); i++){
        if(teams[i].getTeamId() == team_id){
            return teams[i];
        }
    }
    assert(false);
    return teams.front();
}

GameState::GameState(){
    teams.push_back(Team(0, "red"));
    teams.push_back(Team(1, "blue"));
}

GameState::~GameState(){

}

